`
https://leetcode.cn/problems/minimum-absolute-sum-difference/
`

/**
 * @param {number[]} nums1
 * @param {number[]} nums2
 * @return {number}
 */
var minAbsoluteSumDiff = function (nums1, nums2) {
  const n = nums1.length
  const rec = nums1.slice().sort((a, b) => a - b)
  // maxn 存能减少最多的结果
  let sum = 0, maxn = 0
  const MOD = 1e9 + 7
  for (let i = 0; i < n; i++) {
    // 计算出总绝对差值和
    const diff = Math.abs(nums1[i] - nums2[i])
    sum = (sum + diff) % MOD

    // 计算使用当前 i 替换能换到的最大结果
    const j = _.sortedIndex(rec, nums2[i])
    // 算出来左右两边都有可能
    if (j < n) maxn = Math.max(maxn, diff - (rec[j] - nums2[i]))
    if (j > 0) maxn = Math.max(maxn, diff - (nums2[i] - rec[j - 1]))
  }
  // 此时 sum 减去能减最多的结果就是答案
  return (sum - maxn + MOD) % MOD
};